package com.room.search.web.rest;

import com.room.search.core.page.TableDataInfo;
import com.room.search.domain.RoomSecondCityList;
import com.room.search.response.RoomResult;
import com.room.search.service.CityListService;
import com.room.search.service.RoomSecondCityListService;
import com.room.search.service.dto.city.CityConvertDTO;
import com.room.search.service.dto.city.HotCityDTO;
import com.room.search.service.dto.city.QueryCityDTO;
import com.room.search.service.dto.city.SearchCityListDTO;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * REST controller for managing {@link com.room.search.domain.CityList}.
 */
@RestController
@RequestMapping("/api")
public class CityListResource {

    private final CityListService cityListService;
    private final RoomSecondCityListService roomSecondCityListService;

    public CityListResource(CityListService cityListService, RoomSecondCityListService roomSecondCityListService) {
        this.cityListService = cityListService;
        this.roomSecondCityListService = roomSecondCityListService;
    }

    /**
     * 获取所有城市的首字母和省份
     */
    @GetMapping("/initial_cities")
    public ResponseEntity<CityConvertDTO> cityInitial() {
        return ResponseEntity.ok(cityListService.findInitial());
    }

    /**
     * 获取所有城市
     */
    @GetMapping("/all_cities")
    public ResponseEntity<List<SearchCityListDTO>> getAllCities() {
        return ResponseEntity.ok(cityListService.findAll());
    }

    /**
     * 根据省份查询城市
     */
    @GetMapping("/cities/{province}")
    public ResponseEntity<List<SearchCityListDTO>> findByProvince(@PathVariable String province) {
        return ResponseEntity.ok(cityListService.findByProvince(province));
    }

    /**
     * 城市更新接口
     *
     * @param file 文件
     */
    @PostMapping("/cities/upload")
    @ApiOperation("城市列表上传更新接口")
    public ResponseEntity<String> citiesUpload(MultipartFile file) throws IOException {
        cityListService.upload(file);
        return ResponseEntity.ok("success");
    }


    /**
     * 热门城市查询接口
     */
    @GetMapping("/hot_cities")
    @ApiOperation("获取热门城市接口")
    public ResponseEntity<List<HotCityDTO>> getHotCities() {
        return ResponseEntity.ok(cityListService.getHotCities());
    }


    /**
     * 查询所有的城市报考二级的区县
     */
    @GetMapping("/admin_all_cities")
    public TableDataInfo getAdminAllCities(QueryCityDTO dto) {
        return cityListService.getAdminAllCities(dto);
    }

    /**
     * 更新区县列表
     */
    @GetMapping("/update_second")
    public RoomResult updateSecond() {
        return cityListService.updateSecond();
    }


    /**
     * 获取所有区县的列表
     */
    @GetMapping("/all_area")
    public RoomResult getAllArea() {
        return cityListService.getAllArea();
    }

    /**
     * 根据城市获取区县的列表为百度地址展示用
     */
    @GetMapping("/district/{city}")
    public RoomResult getDistrict(@PathVariable String city) {
        return roomSecondCityListService.getDistrict(city);
    }
}
